package com.bat.client.conf;

import cn.hutool.core.collection.CollUtil;
import com.bat.conf.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.concurrent.Future;

//@Configuration
//@Component
//@Aspect
//
//@Order(1)
@Slf4j
public class AsyncAspect {
    @Pointcut("execution(public * com.bat.client.service.*.*(..)) && @annotation(org.springframework.scheduling.annotation.Async)")
    public void asyncAspect(){}
    @Around("asyncAspect()")
    public Object Interceptor(ProceedingJoinPoint pjp){
        log.info("before");
        Object result = null;
        MethodSignature methodSignature=(MethodSignature)pjp.getSignature();
        try {
            result =pjp.proceed();
            log.info("aaaaa");
            if(methodSignature.getReturnType().getName().contains("Future")){
                Future<Object> f= (Future<Object>)result;
                if(CollUtil.isEmpty(BaseController.futureList.get())){
                    BaseController.futureList.set(new ArrayList<>());
                }
                log.info("after");

                BaseController.futureList.get().add(f);
            }
        } catch (Throwable e) {
            log.error("异步切面发生一程",e);
        }
        return result;
    }
}
